//
//  IGTopicContentCell.m
//  jingdongfang
//
//  Created by BOE on 2017/12/14.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGTopicContentCell.h"
#import "IGTopicModel.h"

static NSString *const kTopicContentCellID = @"kTopicContentCellID";
@interface IGTopicContentCell()

@property (nonatomic, strong) UILabel *feedContent;//内容

@end

@implementation IGTopicContentCell

+ (IGTopicContentCell *)cellWithTableView:(UITableView *)tableView
{
    IGTopicContentCell *cell = [tableView dequeueReusableCellWithIdentifier:kTopicContentCellID];
    if (!cell) {
        cell = [[IGTopicContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kTopicContentCellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.feedContent];
        
        [self.feedContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kGAP15);
            make.right.mas_equalTo(-kGAP15);
            make.top.mas_equalTo(12);
            make.height.mas_equalTo(0);
        }];
    }
    return self;
}

- (UILabel *)feedContent
{
    if (!_feedContent) {
        _feedContent = [[UILabel alloc] initWithFrame:CGRectZero];
        _feedContent.font = [UIFont font18];
        _feedContent.textColor = [UIColor c4Color];
        _feedContent.numberOfLines = 0;
        _feedContent.lineBreakMode = NSLineBreakByWordWrapping;
    }
    return _feedContent;
}

- (void)setFeedDetail:(IGTopicModel *)feedDetail
{
    if (_feedDetail == feedDetail) {
        return;
    }
    _feedDetail = feedDetail;
    
    CGFloat height = [IGTopicContentCell feedContentHeight:_feedDetail];
    
    NSMutableParagraphStyle *muStyle = [[NSMutableParagraphStyle alloc]init];
    muStyle.lineSpacing = 3;    //设置行间距离
    muStyle.alignment = NSTextAlignmentLeft;//对齐方式
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:feedDetail.content];
    [attrString addAttribute:NSFontAttributeName value:[UIFont font18] range:NSMakeRange(0, attrString.length)];
    
    [attrString addAttribute:NSParagraphStyleAttributeName value:muStyle range:NSMakeRange(0, attrString.length)];
    self.feedContent.attributedText = attrString;
    self.feedContent.enabled = YES;//设置文字内容是否可变
 
    [self.feedContent mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height - kGAP6*2 - kGAP15);
    }];
}

+ (CGFloat)feedContentHeight:(IGTopicModel *)feedDetail
{
    NSMutableParagraphStyle *muStyle = [[NSMutableParagraphStyle alloc]init];
    muStyle.lineSpacing = 3;    //设置行间距离
    muStyle.alignment = NSTextAlignmentLeft;//对齐方式
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont font18], NSParagraphStyleAttributeName:muStyle};
    CGFloat h = [feedDetail.content boundingRectWithSize:CGSizeMake(KSCREENWIDTH - 2 * kGAP15, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height + 0.5 + kGAP6*2 + kGAP15;

    return h;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
